//
//  DailyProgressViewController.m
//  SecretRoom
//
//  Created by Sitdhibhong Laokok on 9/2/54 BE.
//  Copyright 2554 App Tree. All rights reserved.
//

#import "DailyProgressViewController.h"
#import "ProposeNewEventViewController.h"
#import "TDBadgedCell.h"

@implementation DailyProgressViewController

@synthesize dataSource;
@synthesize context;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithManagedObjectContext:(NSManagedObjectContext *)cx
{
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        self.context = cx;
    }
    
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)loadView
{
    [super loadView];
    
    self.title = @"Events";
    
    self.navigationController.toolbarHidden = NO;
    
    UIBarButtonItem *addNewEventButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewEventButtonClicked:)];
    UIBarButtonItem *showOptionsButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showOptionsButtonClicked:)];
    UIBarButtonItem *flexibleWidth = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    self.toolbarItems = [NSArray arrayWithObjects:addNewEventButton, flexibleWidth, nil];
    
    eventCount = [[DailyEventCounterDelegate alloc] init];
    
    eventViewer = [[EventViewerViewController alloc] initWithManagedObjectContext:self.context];
    
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(showOptionsButtonClicked:) 
                                                 name:@"showOptions" 
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(addNewEventButtonClicked:) 
                                                 name:@"addNewEvent" 
                                               object:nil];
    
    optionsSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Log Out" otherButtonTitles:nil];
    
    proposeNewEvent = nil;
    proposeNewEventNavigation = nil;
    
    [addNewEventButton release];
    [showOptionsButton release];
    [flexibleWidth release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc
{
    [eventViewer release];
    [eventCount release];
    [context release];
    
    if (defaultCellBadgeColor != nil) {
        [defaultCellBadgeColor release];
    }
    
    if (proposeNewEvent != nil) {
        [proposeNewEvent release];
    }
    [super dealloc];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    return section ? 3 : 1 ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    TDBadgedCell *cell = (TDBadgedCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        defaultCellBadgeColor = cell.badgeColor;
    }
    
    int eCount = 0;
    
    if (indexPath.section == 1 && indexPath.row == 0) {
        cell.badgeColor = [UIColor colorWithRed:1.000 green:0.397 blue:0.419 alpha:1.000];
    } else {
        cell.badgeColor = defaultCellBadgeColor;
    }
    
    if (indexPath.section) {
                
        if (indexPath.row == 0) {
            cell.textLabel.text = [SRConnectionControlCenter categoryStringWithCategory:kSREventCategoryOverDue];
            cell.imageView.image = [UIImage imageNamed:@"Overdue.png"];
            eCount = [eventCount numberOfEventWithCategory:kSREventCategoryOverDue inManagedObjectContex:self.context];
            
        } else if (indexPath.row == 1) {
            cell.textLabel.text = [SRConnectionControlCenter categoryStringWithCategory:kSREventCategorySchedule];
            cell.imageView.image = [UIImage imageNamed:@"Schedule.png"];
            eCount = [eventCount numberOfEventWithCategory:kSREventCategorySchedule inManagedObjectContex:self.context];
            
        } else if (indexPath.row == 2) {
            cell.textLabel.text = [SRConnectionControlCenter categoryStringWithCategory:kSREventCategoryLogBook];
            cell.imageView.image = [UIImage imageNamed:@"LogBook.png"];
            eCount = 0; // [eventCount numberOfEventWithCategory:kSREventCategoryLogBook inManagedObjectContex:self.context];
            
        }
    } else {
        cell.textLabel.text = [SRConnectionControlCenter categoryStringWithCategory:kSREventCategoryToday];
        cell.imageView.image = [UIImage imageNamed:@"Today.png"];
        eCount = [eventCount numberOfEventWithCategory:kSREventCategoryToday inManagedObjectContex:self.context];
        
    }
    
    if (eCount > 0) {
        cell.badgeString = [NSString stringWithFormat:@"%i", eCount];
        
    } else {
        cell.badgeString = nil;
    }
    
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [tableView cellForRowAtIndexPath:indexPath].selected = NO;
    
    if (indexPath.section == 0) {
        // inbox
        eventViewer.currentEventDisplay = kSREventCategoryToday;
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            eventViewer.currentEventDisplay = kSREventCategoryOverDue;
        } else if (indexPath.row == 1) {
            eventViewer.currentEventDisplay = kSREventCategorySchedule;
        } else if (indexPath.row == 2) {
            eventViewer.currentEventDisplay = kSREventCategoryLogBook;
        }
    }
    
    [self.navigationController pushViewController:eventViewer animated:YES];
}

- (IBAction)addNewEventButtonClicked:(id)sender 
{
    NSLog(@"%s: New Event", __PRETTY_FUNCTION__);
    
    NSLog(@"Create again");
    proposeNewEvent = [[ProposeNewEventViewController alloc] initWithStyle:UITableViewStyleGrouped];
//    proposeNewEvent.context = self.context;
    proposeNewEventNavigation = [[UINavigationController alloc] initWithRootViewController:proposeNewEvent];
    [self.navigationController presentModalViewController:proposeNewEventNavigation animated:YES];
}

- (IBAction)showOptionsButtonClicked:(id)sender 
{
    [optionsSheet showFromToolbar:self.navigationController.toolbar];
}
@end
